<% var address = JSON.parse(data[0].address)%>
<link href="/styles/mui.picker.css" rel="stylesheet" type="text/css">
<header class="mui-bar mui-bar-nav header">
  <h1 class="mui-title">修改地址</h1>
</header>
<div class="mui-content">
  <form class="mui-input-group">
    <div class="mui-input-row">
      <label>收件人</label>
      <input type="text" class="mui-input-clear" placeholder="" id="receiver" value="<%= address.receiver%>">
    </div>
    <div class="mui-input-row">
      <label>手机</label>
      <input type="number" class="mui-input-clear" placeholder="" id="phone" value="<%= address.phone%>">
    </div>
    <div class="mui-input-row">
      <label>国家</label>
      <input type="text" class="mui-input-clear" placeholder="" id="country" value="<%= address.country%>">
    </div>
    <div class="mui-input-row">
      <label>省市</label>
      <input type="text" class="mui-input-clear" id="city-picker" value="<%= decodeURI(address.city)%>">
    </div>
    <div class="mui-input-row">
      <label>地址</label>
      <input type="text" class="mui-input-clear" placeholder="" id="detail" value="<%= address.location%>">
    </div>
    <div class="mui-button-row">
      <button type="button" class="mui-btn mui-btn-primary" >修改</button>
      <button type="button" class="mui-btn mui-btn-danger" >删除</button>
    </div>
  </form>
</div>
<script src="/js/mui.picker.min.js"></script>
<script src="/js/data.city.js"></script>
<script>
  var city_picker = new mui.PopPicker({layer:3});
  city_picker.setData(init_city_picker);
  $("#city-picker").on("tap", function(){
    setTimeout(function(){
      city_picker.show(function(items){
        $("#city-picker").val((items[0] || {}).text + " " + (items[1] || {}).text + " " + (items[2] || {}).text);
      });
    },200);
  });
</script>
<script>
  $('.mui-btn-primary').click(function () {
//    var is_active = $('.mui-active').val();
//    console.log(typeof(is_active));
//    var is_default = 0;
//    if (typeof(is_active) != 'undefined') {
//      is_default = 1;
//    }
//    console.log(is_default);
    var reqBody = {
      openid: sessionStorage.getItem('openid'),
      name: sessionStorage.getItem('name'),
      phone: sessionStorage.getItem('phone'),
      address: JSON.stringify({
        receiver: $('#receiver').val(),
        phone: $('#phone').val(),
        country: $('#country').val(),
        city: encodeURI($('#city-picker').val()),
        location: $('#detail').val(),
      })
    };
    //console.log(typeof($('#city-picker').val()));
    console.log(reqBody);
    $.ajax({
      url: '/api/user/address/<%= data[0].id%>',
      type: 'PUT',
      data: reqBody,
      success: function (call) {
        if (call == 'success') {
            mui.toast('修改地址成功');
            window.location.href = '/user/address/'
        }
      }
    })
  });
  $('.mui-btn-danger').click(function () {
    var decision = confirm('确认删除？');
    if (decision) {
        $.ajax({
          url: '/api/user/address/<%= data[0].id%>',
          type: 'DELETE',
          success: function (call) {
            console.log(call);
            window.location.href = '/user/address/'
          }
        })
    }
  })

</script>
